/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include <QtGui/QApplication>
#include <QSplashScreen>
#include "mainwindow.h"
#include "plugininterfaces.h"
#include "languageextentioninterface.h"
#include <QtPlugin>
#include <QSettings>
#include <QPluginLoader>
#include <QVector>
#include <QMessageBox>
#include "globals.h"
#include "qtwin.h"

QVector<Plugin*> *plugins;
QVector<Language*> *languages;
QSettings settings;

QVector<Plugin*>* loadPlugins(QSplashScreen* splash);
QVector<Language*> *loadExtensions(QSplashScreen* splash);

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setEffectEnabled(Qt::UI_AnimateCombo);

    /*
      Application Information
    */
    QCoreApplication::setOrganizationName("SSC");
    QCoreApplication::setOrganizationDomain("http://edwardoid.com");
    QCoreApplication::setApplicationName("BlackRabbit");
    QCoreApplication::setApplicationVersion("0.1");
    /*
      <----
      */
    QSettings settings; // Defining extern variable settings (globals.h) <----

    /*
      Defining if it needs plugins directory.
      */
    if(settings.
       value("Plugins directory", "null")
       .toString().compare("null") == 0)
       {
        QString path = QFileDialog::getExistingDirectory(0,
                                          "Select plugins directory",
                                          qApp->applicationDirPath());
        settings.setValue("Plugins directory", path);
    }
    /*
      <----
      */

    /*
      Defining if it needs extentions directory.
      */
    if(settings.
       value("Extentions directory", "null")
       .toString().compare("null") == 0)
       {
        QString path = QFileDialog::getExistingDirectory(0,
                                          "Select extentions directory",
                                          qApp->applicationDirPath());
        settings.setValue("Extentions directory", path);
    }
    /*
      <----
      */

    QPixmap splashImg("res/splash.jpg");
    QSplashScreen splash(splashImg);
    splash.show();
        app.processEvents();
        MainWindow w(0);
        plugins = loadPlugins(&splash);
        languages = loadExtensions(&splash);
        /*
            SETTING WINDOW STYLE LIKE Aero ON WINDOWS VISTA/7
        #ifdef Q_WS_X11
            w.setAttribute(Qt::WA_TranslucentBackground);
            w.setAttribute(Qt::WA_NoSystemBackground, false);
            QPalette pal = w.palette();
            QColor bg = pal.window().color();
            bg.setAlpha(180);
            pal.setColor(QPalette::Window, bg);
            w.setPalette(pal);
            w.ensurePolished(); // workaround Oxygen filling the background
            w.setAttribute(Qt::WA_StyledBackground, false);
        #endif
        if (QtWin::isCompositionEnabled()) {
            QtWin::extendFrameIntoClientArea(&w);
            w.setContentsMargins(0, 0, 0, 0);
        }
        */
        w.showMaximized();
    splash.finish(&w);
    return app.exec();
}


QVector<Plugin*>* loadPlugins(QSplashScreen *splash)
{
    QVector<Plugin*> *pluginsLoaded = new QVector<Plugin*>;
    QDir pluginsDir(settings.value("Plugins directory",qApp->applicationDirPath()).toString());
    #if defined(Q_OS_WIN)
     if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
         ;//pluginsDir.cdUp();
    #elif defined(Q_OS_MAC)
     if (pluginsDir.dirName() == "MacOS") {
         pluginsDir.cdUp();
         pluginsDir.cdUp();
         pluginsDir.cdUp();
     }
    #endif
    //pluginsDir.cd("plugins");
    qDebug("Trying load plugins:");
    foreach(QString filename, pluginsDir.entryList(QDir::Files))
    {
        QPluginLoader loader(pluginsDir.absoluteFilePath(filename));
        QObject *plugin = loader.instance();
        if(plugin)
        {
            splash->showMessage(filename.append(" loaded!(plugin)"));
            pluginsLoaded->push_back(qobject_cast<Plugin*>(plugin));
        }
        else
           ;// QMessageBox::critical(0, "Error", loader.errorString());
    }
    qDebug("Plugins loaded.\n<------>");
    return pluginsLoaded;
}

QVector<Language*> *loadExtensions(QSplashScreen *splash)
{
    QVector<Language*> *extensionsLoaded = new QVector<Language*>;
    QDir pluginsDir(settings.value("Extention directory", qApp->applicationDirPath()).toString());
    #if defined(Q_OS_WIN)
     if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
         pluginsDir.cdUp();
    #elif defined(Q_OS_MAC)
     if (pluginsDir.dirName() == "MacOS") {
         pluginsDir.cdUp();
         pluginsDir.cdUp();
         pluginsDir.cdUp();
     }
    #endif
    pluginsDir.cd("extensions");
    qDebug("Trying load extensions:");
    foreach(QString filename, pluginsDir.entryList(QDir::Files))
    {
        QPluginLoader loader(pluginsDir.absoluteFilePath(filename));
        QObject *extention = loader.instance();
        if(extention)
        {
            splash->showMessage(filename.append(" loaded!(Extention)"));
            extensionsLoaded->push_back(qobject_cast<Language*>(extention));
        }
        else
        {
            qDebug(loader.errorString().toStdString().c_str());
        }
    }

    return extensionsLoaded;
}


